
<template >

  <div style="height:6vh;padding: 10px 20px;background-color:#fff;">
    <span style="line-height: 60px;font-size:20px;">用户管理</span>
    <el-button type="primary" style="float:right;margin-top: 10px;"
    @click="beforeAddUser">新建用户</el-button>
  </div>

  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="用户名">
            <el-input placeholder="请输入用户名" v-model="saveUserForm.username"
                      :disabled="saveUserForm.id!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input placeholder="请输入手机号" v-model="saveUserForm.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input placeholder="请输入邮箱" v-model="saveUserForm.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄">
            <el-input placeholder="请输入年龄" v-model="saveUserForm.age"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="头像">
            <el-upload
                v-model:file-list="fileList"
                v-model:action="saveUserForm.avatar"
                name="file"
                limit="1"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
            >
              <el-icon>
                <Plus/>
              </el-icon>
            </el-upload>
            <el-dialog v-model="dialogVisibletop">
              <img w-full :src="dialogImageUrl"/>
            </el-dialog>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="saveUserForm.gender">
              <el-radio border label="男" value="1" style="margin: 0;"></el-radio>
              <el-radio border label="女" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户状态">
            <el-radio-group v-model="saveUserForm.status">
              <el-radio border label="启用" value="1" style="margin: 0;"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>


  <template #footer>
    <el-button @click="handleClose1">取消</el-button>
    <el-button type="primary" @click="saveUser">保存</el-button>
  </template>
 </el-dialog>

  <!-- 用户搜索卡片 -->
  <el-card style="height: 70px;margin: 20px;">
    <el-form :inline="true">
      <el-form-item label="用户搜索" style="width: 220px;">
        <el-input placeholder="输入用户名" v-model="searchUserForm.username"></el-input>
      </el-form-item>
      <el-form-item label="用户状态" style="width: 290px;" >
        <el-select placeholder="请选择用户状态" v-model="searchUserForm.status">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button >重置</el-button>
        <el-button type="primary" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 用户列表表格 -->
  <el-card style="margin: 20px;">
    <el-table :data="userArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column label="用户名" prop="username" align="center"></el-table-column>
      <el-table-column label="性别" prop="gender" align="center" :formatter="userGenderFormatter"></el-table-column>
      <el-table-column label="头像" prop="avatar" align="center">
        <template #default="scope">
          <img :src="scope.row.avatar" width="50" height="50">
        </template>
      </el-table-column>
      <el-table-column label="电话" prop="phone" align="center"></el-table-column>
      <el-table-column label="邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0" v-model="scope.row.level "  @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary"
                    >密码重置</el-button>
          <el-button link type="primary" @click="editUser(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>



</template>

<script setup>
//定义变量用来控制弹窗是否可见
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const dialogVisible = ref(false);
//定义变量用来控制弹窗标题
const dialogTitle = ref('新建用户');

//
const handleClose = (done) => {
  dialogVisible.value = false;
  done();
}
const beforeAddUser = ()=>{
  dialogTitle.value = '新建用户';
  dialogVisible.value = true;
}
const userGenderFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '男';
  }else if(cellValue == 0){
    cellValue = '女';
  }
  return cellValue;
}

//定义对象用来收集用户表单数据
const saveUserForm = ref({
  username:'',
  phone:'',
  avatar:"",
  email:'',
  age:'',
  level:'',
  parentId:'',
  gender:'',
  status:''
});

const avatarUrl = ref('');

const saveUser = ()=>{
  //console.log(saveUserForm.value);
  saveUserForm.value.avatar= fileList.value[0].response.data;
  //avatarUrl.value=BASE_URL+fileList.value[0].response.data;
  //saveUserForm.value.avatar=dialogImageUrl.value;
  let data = qs.stringify(saveUserForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/user/save',data).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success('保存成功!');
      saveUserForm.value = {};//保存成功后要清空弹窗表单并关闭弹窗
      dialogVisible.value = false;
      //新增成功后,需要重新加载所有的用户数据
      loadUser();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

/**图片上传相关代码开始**/
const fileList = ref([]);
const dialogImageUrl = ref('');
const dialogVisibletop = ref(false);
//定义变量用来保存图片上传给哪个后端接口地址
const actionUrl = ref(BASE_URL + '/v1/file/upload');
//移除图片
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  //1.得到要删除的图片路径 uploadFile就是我们要删除的那个图片文件对象
  let imgUrl = uploadFile.response.data;
  //2.给后端发请求,删除图片
  axios.post(BASE_URL+'/v1/file/remove?imgUrl='+imgUrl)
      .then((response)=>{
        if (response.data.code==2000){
          ElMessage.success('删除成功!');
        }
      })
}
//图片上传后预览
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url
  saveUserForm.value.avatar = uploadFile.url
  dialogVisible.value = true;
  console.log(uploadFile);
  //console.log(fileList.value[0].response.data);
}
/**图片上传相关代码结束**/

onMounted(()=>{
  loadUser();
})
//定义变量用来保存用户搜索条件
const searchUserForm = ref({username:'',status:''});
const userArr = ref([]);


//查询用户
const loadUser = ()=>{
  //给后端发请求,查出此职级对应的所有人
  axios.get(BASE_URL+'/v1/user/select',searchUserForm.value).then((response)=>{
    if(response.data.code==2000){
      userArr.value = response.data.data;
      console.log(userArr.value);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义用户删除的方法
const deleteUser = (userId)=>{
  if(confirm('您确认要删除当前员工吗?')){
    axios.post(BASE_URL+'/v1/user/delete/'+userId).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success('删除成功!');
        //删除成功后,重新加载所有用户数据
        loadUser();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}

const changeStatus = (userId,status)=>{
  axios.post(BASE_URL+'/v1/user/update/status/'+userId+'/'+status)
      .then((response)=>{
        if(response.data.code==2000){
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}

//定义编辑用户的方法
const editUser = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑用户';
  axios.get(BASE_URL+'/v1/user/select?id='+id).then((response)=>{
    if(response.data.code==2000){
      saveUserForm.value = response.data.data[0];
      console.log("编辑"+JSON.stringify(saveUserForm.value))
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//定义处理弹窗关闭的方法
const handleClose1 = ()=>{
  if(confirm('您确认要关闭弹窗吗?')){
    saveUserForm.value = {};
    dialogVisible.value = false;
  }
}

</script>

<style scoped>

</style>